#!/bin/sh

if [ $# -lt 1 ]; then
    echo "Error==>Need args,eg pdf2pdf_fxcore.x64.mt.25581.1599"
    exit 1
fi

RPM_TO_FZ_YUM_FXCORE_VERSION=$1
RPM_BUILD_SH_PATH=./rpm_build.sh
RPM_FZ_YUM_REPO=root@10.103.16.251:/data/yum_repo/Centos-6/foxit
RPM_FZ_YUM_REPO_PORT=22
RPM_BUILD_SH_OUTPUT_FOLDER=YumOutput
RPM_TO_FZ_YUM_SOURCE_FOLDER=../$RPM_BUILD_SH_OUTPUT_FOLDER
#RPM_TO_FZ_YUM_SOURCE_FOLDER=../output

if [ ! -f "$RPM_BUILD_SH_PATH" ]; then
    echo "Error===>No rpm build shell script: $RPM_BUILD_SH_PATH";
    exit 1
fi 

if [ ! -x "$RPM_BUILD_SH_PATH" ]; then
   echo "Warning===>$RPM_BUILD_SH_PATH has no x permission,modify it now"
   chomd +x $RPM_BUILD_SH_PATH
   if [ $? -ne 0 ]; then
      echo "Error===>chmod x permission failure: $RPM_BUILD_SH_PATH";
      exit 1
   fi 
fi

#backup output
RPM_TO_FZ_YUM_OUTPUT_FOLDER=$RPM_TO_FZ_YUM_SOURCE_FOLDER
RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK=$RPM_TO_FZ_YUM_OUTPUT_FOLDER.bak
RPM_TO_FZ_YUM_OUTPUT_FOLDER_FILES=`ls $RPM_TO_FZ_YUM_OUTPUT_FOLDER`
if [ -d "$RPM_TO_FZ_YUM_OUTPUT_FOLDER" ]; then 
    echo "Info===>Find user's old output folder,backup it now"
    if [ -n "$RPM_TO_FZ_YUM_OUTPUT_FOLDER_FILES" ]; then
      echo "Info===>Find user's old output folder is not empty,backup it now"
      mv $RPM_TO_FZ_YUM_OUTPUT_FOLDER $RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK
      if [ $? -ne 0 ]; then
        echo "Error==>Backup user's old output folder failure,delete it now"
        rm -rf $RPM_TO_FZ_YUM_OUTPUT_FOLDER
      fi
   fi
fi 

# rpm build shell
$RPM_BUILD_SH_PATH $RPM_TO_FZ_YUM_FXCORE_VERSION $RPM_BUILD_SH_OUTPUT_FOLDER

RPM_TO_FZ_YUM_RPM_NAME=`ls $RPM_TO_FZ_YUM_SOURCE_FOLDER | grep $RPM_TO_FZ_YUM_FXCORE_VERSION`
echo "grep rpm name result:$RPM_TO_FZ_YUM_RPM_NAME"
if  [ -n "$RPM_TO_FZ_YUM_RPM_NAME" ] ;then 
   RPM_TO_FZ_YUM_FINAL_RPM_PATH=$RPM_TO_FZ_YUM_SOURCE_FOLDER/$RPM_TO_FZ_YUM_RPM_NAME
   echo "RPM_TO_FZ_YUM_FINAL_RPM_PATH find value:$RPM_TO_FZ_YUM_FINAL_RPM_PATH"
fi

echo "Info==>RPM path: $RPM_TO_FZ_YUM_FINAL_RPM_PATH"
if [ -f "$RPM_TO_FZ_YUM_FINAL_RPM_PATH" ]; then
    echo "Info==>RPM path: $RPM_TO_FZ_YUM_FINAL_RPM_PATH exist"
    scp -P $RPM_FZ_YUM_REPO_PORT $RPM_TO_FZ_YUM_FINAL_RPM_PATH $RPM_FZ_YUM_REPO
else
    ALL_FILES=*
    echo "Info==>RPM path: $RPM_TO_FZ_YUM_FINAL_RPM_PATH not exist,move all output's files to repo" 
    scp -P $RPM_FZ_YUM_REPO_PORT $RPM_TO_FZ_YUM_SOURCE_FOLDER/$ALL_FILES $RPM_FZ_YUM_REPO
fi

if [ $? -ne 0 ]; then
    echo "Error===>scp to: $RPM_FZ_YUM_REPO failure";
else
    echo "Info==>scp to yum server success";
fi 

echo "Try Restore backup output folder begin"
echo "RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK: $RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK"
if  [ -n "$RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK" ] ;then 
    echo "RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK not empty"
    if [ -d "$RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK" ]; then 
      echo "Prepare restore backup output folder: $RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK"
      if [ -d "$RPM_TO_FZ_YUM_OUTPUT_FOLDER" ];then
        echo "remove temp output folder: $RPM_TO_FZ_YUM_OUTPUT_FOLDER"
        rm -rf $RPM_TO_FZ_YUM_OUTPUT_FOLDER
      fi
      echo "restore source output folder: $RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK -> $RPM_TO_FZ_YUM_OUTPUT_FOLDER"
      mv $RPM_TO_FZ_YUM_OUTPUT_FOLDER_BAK $RPM_TO_FZ_YUM_OUTPUT_FOLDER
    fi 
fi
echo "Try Restore backup output folder end"

if [ -d "$RPM_TO_FZ_YUM_OUTPUT_FOLDER" ]; then
  echo "remove temp yum out put folder: $RPM_TO_FZ_YUM_OUTPUT_FOLDER"
  rm -rf $RPM_TO_FZ_YUM_OUTPUT_FOLDER
fi

echo "3Q,Complete transfter files to yum server"


